package Case;

import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-06-30
 * Time: 20:36
 */
public class Deom2 {
    public static void main(String[] args) throws IOException {
        //1.输入必要的信息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入当前文件的路径：");
        String srcPath = scanner.nextLine();
        System.out.println("请输入目标文件的路径：");
        String destPath = scanner.nextLine();
        //2.检查文件是否合法
        File srcFile = new File(srcPath);
        if(!srcFile.isFile()) {
            System.out.println("当前文件的路径不合法");
            return;
        }
        //目标文件有可能不存在，所以我们判断目标文件上一级目录是否存在即可
        File destFile = new File(destPath);
        if(!destFile.getParentFile().isDirectory()) {
            System.out.println("目标文件上一级目录的路径不合法");
            return;
        }
        //读写操作
        try(InputStream inputStream = new FileInputStream(srcFile);
        OutputStream outputStream = new FileOutputStream(destFile)) {
            while(true) {
                byte[] bytes = new byte[1024];
                //读文件
                int b = inputStream.read(bytes);
                if(b == -1) {
                    break;
                }
                //写文件
                outputStream.write(bytes,0,b);
            }
        }
    }
}
